package bb.lanxing.mvp.presetner

import bb.lanxing.App
import bb.lanxing.R
import bb.lanxing.activity.interfaces.ILoginModel
import bb.lanxing.activity.interfaces.ILoginPresenter
import bb.lanxing.activity.interfaces.ILoginView
import bb.lanxing.model.database.User
import bb.lanxing.mvp.presetner.i.BasePresenter
import com.umeng.socialize.bean.SHARE_MEDIA
import org.json.JSONObject
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers

class LoginPresenterImpl(private val view: ILoginView) : BasePresenter(), ILoginPresenter {
    private val model: ILoginModel = LoginModelImpl()

    override fun login(account: String, pwd: String) {
        view.showLoadingDialog(R.string.mine_login_dialog_signing, true)
        val login = model.login(account, pwd)
        addSubscription(
            login.observeOn(AndroidSchedulers.mainThread()).subscribe(object : Subscriber<User>() {
                override fun onCompleted() {
                }

                override fun onError(th: Throwable) {
                    view.dismissLoadingDialog()
                    App.getContext().showMessage(th.message)
                    th.printStackTrace()
                }

                override fun onNext(user: User) {
                    pro()
                    view.dismissLoadingDialog()
                    view.onLoginResult(user)
                }
            })
        )
    }

    override fun loginSms(account: String, code: String) {
        view.showLoadingDialog(R.string.mine_login_dialog_signing, true)
        val login = model.loginSms(account, code)
        addSubscription(
            login.observeOn(AndroidSchedulers.mainThread()).subscribe(object : Subscriber<User>() {
                override fun onCompleted() {
                }

                override fun onError(th: Throwable) {
                    view.dismissLoadingDialog()
                    App.getContext().showMessage(th.message)
                    th.printStackTrace()
                }

                override fun onNext(user: User) {
                    pro()
                    view.dismissLoadingDialog()
                    view.onLoginResult(user)
                }
            })
        )
    }

    override fun loginByThirdParty(share_media: SHARE_MEDIA, jSONObject: JSONObject) {
        addSubscription(
            model.loginByThirdParty(share_media, jSONObject)
                .observeOn(AndroidSchedulers.mainThread()).subscribe(object : Subscriber<User>() {
                    override fun onCompleted() {
                        view.dismissLoadingDialog()
                    }

                    override fun onError(th: Throwable) {
                        view.dismissLoadingDialog()
                        App.getContext().showMessage(th.message)
                        th.printStackTrace()
                    }

                    override fun onNext(user: User) {
                        view.dismissLoadingDialog()
                        try {
                            pro()
                            view.onLoginByThirdPartyResult(user)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                })
        )
    }

    private fun pro() {
    }
}